class Image < DatabaseObject

  attr_accessor :id, :url, :title,
  :description, :time_uploaded

  def initialize(user, gallery, title, description = "No description.")
    @id = $database.images.last.id + 1
    @user = user.id
    @gallery = gallery.id
    @title = title
    @description = description
    @time_uploaded = current_time

    raise "user must be User instance" unless user.class == User
    raise "gallery must be Gallery instance" unless gallery.class == Gallery
    raise "@user must be Integer" unless @user.kind_of? Integer
    raise "@gallery must be Integer" unless @gallery.kind_of? Integer
  end

  def rating
    this_rating  = 0
    this_num_votes = 0

    $database.votes.each {|vote|
      if vote.image.id == @id
        this_num_votes += 1
        this_rating += 1 if vote.grade == true
      end
    }
    bayesian_average(this_rating, this_num_votes)
  end

end